/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.provider;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.slice.PaintAilitySlice;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.filters.OilPaintFilter;
import com.cv4j.core.filters.StrokeAreaFilter;
import com.cv4j.rxcv4j.RxImageData;

import java.util.Optional;

/**
 * ViewCreateHelper
 *
 * @since 2021-05-20
 */
public class ViewCreateHelper {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "ViewCreateHelper");
    private static final int THREE = 3;
    private static final int ONE = 1;
    private static final int TWO = 2;
    private static final int FOUR = 4;
    private static final int SIXTY = 60;
    private static final int FIVE = 5;
    private static final int TWOZEROZEROZERO = 2000;
    private static final int TWOZERO = 6;
    private Context slice;

    /**
     * ViewCreateHelper
     *
     * @param abilitySlice abilitySlice
     * @param mainBannerAbilitySlice mainBannerAbilitySlice
     */
    public ViewCreateHelper(Context abilitySlice, PaintAilitySlice mainBannerAbilitySlice) {
        this.slice = abilitySlice;
    }

    /**
     * createViewHome
     *
     * @param title title
     * @return rootLayout
     */
    public Optional<Component> createViewHome(String title) {
        Component mainComponent =
            LayoutScatter.getInstance(slice).parse(
                ResourceTable.Layout_ability_compare_hist_item, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return Optional.empty();
        }
        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        Image compageImage = (Image) rootLayout.findComponentById(ResourceTable.Id_compageImage);
        compageImage.setPixelMap(ResourceTable.Media_test_oil_paint);
        return Optional.ofNullable(rootLayout);
    }

    /**
     * createViewPage
     *
     * @param title title
     * @return rootLayout
     */
    public Optional<Component> createViewPage(String title) {
        Component mainComponent =
            LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_ability_compare_hist_item, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return Optional.empty();
        }
        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        Image compageImage = (Image) rootLayout.findComponentById(ResourceTable.Id_compageImage);
        RxImageData.bitmap(PixelMapUtil.getPixelMapFromResource(slice, ResourceTable.Media_test_oil_paint).get())
            .addFilter(new OilPaintFilter())
            .into(compageImage);
        return Optional.ofNullable(rootLayout);
    }

    /**
     * createViewPage
     *
     * @param title title
     * @return rootLayout
     */
    public Optional<Component> createViewIndicator(String title) {
        Component mainComponent =
            LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_ability_compare_hist_item, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return Optional.empty();
        }
        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        Image compageImage = (Image) rootLayout.findComponentById(ResourceTable.Id_compageImage);
        RxImageData.bitmap(PixelMapUtil.getPixelMapFromResource(slice, ResourceTable.Media_test_oil_paint).get())
            .addFilter(new StrokeAreaFilter())
            .into(compageImage);
        return Optional.ofNullable(rootLayout);
    }

}
